
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QMessageBox>

#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>

#include "addgrabtndialog.h"
#include "global.h"

AddGraBtnDialog::AddGraBtnDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle(QString("添加手控点"));

    create_three_groupbox();
    //按钮区域
    add_btn = new QPushButton(QString("确定"));
    cancel_btn = new QPushButton(QString("取消"));
    close_btn = new QPushButton(QString("关闭"));
    button_box = new QDialogButtonBox;
    button_box->addButton(add_btn, QDialogButtonBox::ActionRole);
    button_box->addButton(cancel_btn, QDialogButtonBox::ActionRole);
    button_box->addButton(close_btn, QDialogButtonBox::ActionRole);

    connect(add_btn, SIGNAL(clicked()), this, SLOT(add_btn_clicked()));
    connect(cancel_btn, SIGNAL(clicked()), this , SLOT(cancel_btn_clicked()));
    connect(close_btn, SIGNAL(clicked()), this, SLOT(close()));

    //将设备类型和数据库关联
    table_model= new QSqlTableModel(this);
    table_model->setTable("device_logoinf_table");
    table_model->select();
    device_name_combox->setModel(table_model);
    device_name_combox->setModelColumn(0);

    //设置预览区域和操作说明区域的尺寸策略
    QSizePolicy policy = preview_area->sizePolicy();
    policy.setVerticalStretch(2);
    preview_area->setSizePolicy(policy);

    policy = help_groupbox->sizePolicy();
     policy.setVerticalStretch(1);
    help_groupbox->setSizePolicy(policy);

    //布局管理
    //left layout
    QVBoxLayout *leftlayout = new QVBoxLayout;
    leftlayout->addWidget(edit_groupbox);
    //right layout
    QVBoxLayout *rightlayout = new QVBoxLayout;
    rightlayout->addWidget(preview_area);
    rightlayout->addWidget(help_groupbox);
    //top layout
    QHBoxLayout *toplayout =  new QHBoxLayout;
    toplayout->addLayout(leftlayout);
    toplayout->addLayout(rightlayout);
    //bottom layout
    QHBoxLayout *bottomlayout =  new QHBoxLayout;
    bottomlayout->addWidget(button_box);
    //main layout
    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addLayout(toplayout);
    mainlayout->addLayout(bottomlayout);
    setLayout(mainlayout);
}


void AddGraBtnDialog::create_three_groupbox()
{
    edit_groupbox = new QGroupBox(QString("编辑信息"));
    district = new QLineEdit;
    district_label = new QLabel(QString("区  号"));
    district_label->setBuddy(district);
    building = new QLineEdit;
    building_label = new QLabel(QString("栋  号"));
    building_label->setBuddy(building);
    layer = new QLineEdit;
    layer_label = new QLabel(QString("层  号"));
    layer_label->setBuddy(layer);
    room = new QLineEdit;
    room_label = new QLabel(QString("房  号"));
    room_label->setBuddy(room);
    device_name_combox = new QComboBox;
    device_name_label = new QLabel(QString("设备类型"));
    device_name_label->setBuddy(device_name_combox);

    preview_area = new QGroupBox(QString("手控点预览"));
    preview_btn = new GraphicButton(-1,this);
    preview_btn->init_const_member();
    preview_btn->set_geo_description_and_num(QString("xx区xx栋xx层xx房  xxx"), QString("xxxx"));
    preview_btn->set_start_led(TURN_OFF);
    preview_btn->set_fedback_led(TURN_OFF);
    preview_btn->set_fault_led(TURN_OFF);
    preview_btn->set_device_logo("./image/device_icon/hb.jpg");

    help_groupbox = new QGroupBox(QString("操作说明"),this);
    help_list = new QListWidget(this);
    QStringList help_inf ;
    help_inf<<"01: 输入 区/栋/层/房 号,不带汉字 区/栋/层/房"
		   <<"02: 选择要添加的手控点的设备类型"
		   <<"03: 如果没有所需设备类型,需在手控点设置界面添加";
    help_list->addItems(help_inf);
    help_list->show();

    QHBoxLayout *layout1 = new QHBoxLayout;
    layout1->addWidget(district_label);
    layout1->addWidget(district);
    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addWidget(building_label);
    layout2->addWidget(building);
    QHBoxLayout *layout3 = new QHBoxLayout;
    layout3->addWidget(layer_label);
    layout3->addWidget(layer);
    QHBoxLayout *layout4 = new QHBoxLayout;
    layout4->addWidget(room_label);
    layout4->addWidget(room);
    QHBoxLayout *layout5 = new QHBoxLayout;
    layout5->addWidget(device_name_label);
    layout5->addWidget(device_name_combox);

    QVBoxLayout *edit_layout = new QVBoxLayout;
    edit_layout->addLayout(layout1);
    edit_layout->addLayout(layout2);
    edit_layout->addLayout(layout3);
    edit_layout->addLayout(layout4);
    edit_layout->addLayout(layout5);
    edit_groupbox->setLayout(edit_layout);

    QVBoxLayout *help_layout = new QVBoxLayout;
    help_layout->addWidget(help_list);
    help_groupbox->setLayout(help_layout);

    QHBoxLayout *preview_layout = new QHBoxLayout;
    preview_layout->addWidget(preview_btn);
    preview_area->setLayout(preview_layout);
}



void AddGraBtnDialog::add_btn_clicked()
{
    QString distric_num = district->text() + "区";
    QString building_num = building->text() + "栋";
    QString layer_num = layer->text() + "层";
    QString room_num = room->text() + "房";
    QString dev_name = device_name_combox->currentText();

    if( input_valid(distric_num,building_num,layer_num,room_num) )
    {
        int id = num_allocate.allocate_num();
        if( id<0 || id>MAX_NUM_GRA_BTN )
        {
            qDebug()<<"allocate id failed";
        }
        qDebug()<<id<<distric_num<<building_num<<layer_num<<room_num<<dev_name;
        QSqlQuery add_query;

        bool  flag = add_query.exec( QString( "insert into locate_information_table values (%1, '%2', '%3', '%4', '%5',  '%6', 0) "
                                              ).arg(id).arg(distric_num).arg(building_num).arg(layer_num).arg(room_num).arg(dev_name) );
        if (!flag)
        {
            QSqlError lasterror = add_query.lastError();
            qDebug()<<lasterror.driverText();
            return;
        }
        NodeStatus::Instance()->add(id,0);
        QMessageBox::information( this, "", QString("add sucessfull!") );
        district->clear();
        building->clear();
        layer->clear();
        room->clear();
    }
    else
    {
        QMessageBox::information( this, "输入信息", QString("请输入区/栋/层/房号") );
    }

}

bool AddGraBtnDialog::input_valid(QString &dis, QString &buil, QString &layer, QString &room)
{
    return true;
}



void AddGraBtnDialog::cancel_btn_clicked()
{

}
